﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SqlReplace
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            txtFileName.Text = GetNameWithPath();
            ReadFile(txtFileName.Text);


        }
        private string GetNameWithPath()
        {
            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Filter = "dalcs文件(*.cs)|*.cs";
            if (openDlg.ShowDialog() == DialogResult.OK)
            {
                return openDlg.FileName;
            }
            else
            {
                return string.Empty;
            }
        }
      
        private void ReadFile(string fileNameWithPath)
        {
            if (string.IsNullOrWhiteSpace(fileNameWithPath))
            {
                return;
            }
            RegHelper.Init();


            using (StreamReader sr = new StreamReader(fileNameWithPath, Encoding.UTF8))
            {
                string lineData;
                List<string> methodList = new List<string>();

                while ((lineData = sr.ReadLine()) != null)
                {
                    RegHelper.CheckData(lineData);
                    //RegHelper.DealData(lineData, methodList);
                }
                //最后一个class 下面在没有class了，不会触发添加方法，手动判断添加下
                RegHelper.AddMethodListToClassList(methodList);
            }
            DisplayData(RegHelper.fileContent);
        }
       
       
        public void DisplayData(StringBuilder fileContent)
        {
           // txtRecord.Text = fileContent.ToString();
            var matchsb = new StringBuilder();
            foreach (var item in RegHelper.matchContent)
            {
                matchsb.AppendLine(item);
            }
            txtChange.Text = matchsb.ToString();
            txtRecord.Text = RegHelper.otherParam.ToString();
        }

        private void btnBegin_Click(object sender, EventArgs e)
        {
            using (StreamWriter sw = new StreamWriter(txtFileName.Text, false, Encoding.UTF8))
            {
                sw.Write(RegHelper.fileContent);
            }
            MessageBox.Show("替换成功");
        }
    }
}
